package cn.tedu.csmall.sentinel.study.controller;

import cn.tedu.csmall.sentinel.study.block.GlobalBlockExceptionHandler;
import cn.tedu.csmall.sentinel.study.service.HelloService;
import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@Slf4j
public class HelloController {
    /**
     * 请求路径 /hello
     * get请求
     * String name
     * String result
     */
    @Autowired
    private HelloService helloService;

    @GetMapping("/hello")
    @SentinelResource(
            value="sayHi",
            blockHandler = "sayHiBlockHandle",
            blockHandlerClass = GlobalBlockExceptionHandler.class
    )
    public String sayHi(String name) throws IOException {
        //准备资源入口
        /*Entry entry = null;*/
        String sayHelloWords= null;
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        sayHelloWords=helloService.doSayHi(name);
        /*try{
            //开启资源保护的入口 entry赋值,资源名称不能重复
            entry = SphU.entry("sayHi");
            sayHelloWords=helloService.doSayHi(name);
        }catch (BlockException e){
            //更具我们定义的规则,流控规则,需要对资源调用
            //进行流控异常的捕获
            //可以异常捕获时,表示当前资源访问已经违反,达到规则的临界点
            sayHelloWords="达到限流规则阈值,blocked";
        }catch(Throwabl e){

        }finally {
            //释放资源入口
            if(entry != null){
                entry.exit();
            }
        }*/
        return sayHelloWords;
    }
}
